package cos.android.voleysplash.level;

import java.io.IOException;
import java.util.Properties;
import java.util.Vector;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import cos.android.voleysplash.R;
import cos.android.voleysplash.element.Brick;
import cos.android.voleysplash.element.Element;
import cos.android.voleysplash.element.Elements;
import cos.android.voleysplash.objects.Ball;

public class Level implements Cloneable {

	protected Vector<Element> elements;

	private float gravity = 0;
	Properties properties;
	private Element keyElement;

	int canvasHeight;
	int canvasWidth;

	public Level(Resources res, Properties properties, int level, int world,
			int canvasHeight, int canvasWidth) {

		gravity = Float.parseFloat((String) properties.get("gravity"));
		this.properties = properties;
		this.canvasHeight = canvasHeight;
		this.canvasWidth = canvasWidth;
		elements = new Vector<Element>();
		XmlResourceParser parser = null;

		parser = res.getXml(R.xml.levels);

		try {

			parser.next();
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG
						&& parser.getName().equals("level")) {

					while (!(eventType == XmlPullParser.END_TAG && parser
							.getName().equals("level"))) {

						if (eventType == XmlPullParser.START_TAG) {
							if (parser.getName().equals("ballPos")) {
								Element element = null;
								switch (Elements.valueOf(parser
										.getAttributeValue(0))) {
								case Ball:
									element = new Ball(
											BitmapFactory.decodeResource(res,
													R.drawable.tennisball),
											BitmapFactory.decodeResource(res,
													R.drawable.tennisball),
											parser.getAttributeIntValue(2, 0),
											parser.getAttributeIntValue(3, 0),

											Float.parseFloat((String) properties
													.get("tenisBallFlexibility")),
											Float.parseFloat((String) properties
													.get("tenisBallWeight")),
											"TenisBall");
									break;
								}
								element.setName(parser.getAttributeValue(1));
								keyElement = element;
								elements.add(element);
							} else if (parser.getName().equals("bricks")) {
								while (!(eventType == XmlPullParser.END_TAG && parser
										.getName().equals("bricks"))) {
									if (eventType == XmlPullParser.START_TAG
											&& parser.getName().equals("line")) {

										for (int i = 0; i < parser
												.getAttributeIntValue(2, 0); i++) {
											Element element = null;

											element = new Brick(
													BitmapFactory
															.decodeResource(
																	res,
																	R.drawable.brick),
													BitmapFactory
															.decodeResource(
																	res,
																	R.drawable.brick),
													i
															* 40
															+ parser.getAttributeIntValue(
																	0, 0),
													parser.getAttributeIntValue(
															1, 0), "Brick");
											element.setName("Brick"
													+ i
													+ " "
													+ parser.getAttributeIntValue(
															1, 0));
											elements.add(element);
										}

									}
									eventType = parser.next();
								}
							}
						}
						eventType = parser.next();
					}
				}

				eventType = parser.next();
			}

		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Vector<Element> getElements() {
		return elements;
	}

	public void setElements(Vector<Element> elements) {
		this.elements = elements;
	}

	public int getCanvasHeight() {
		return canvasHeight;
	}

	public void setCanvasHeight(int canvasHeight) {
		this.canvasHeight = canvasHeight;
	}

	public int getCanvasWidth() {
		return canvasWidth;
	}

	public void setCanvasWidth(int canvasWidth) {
		this.canvasWidth = canvasWidth;
	}

	public void updateGame(float elapsed, Canvas canvas) {

		checkElements();
		updateGravity(elapsed);
		checkCollides();
		// checkInteractions(elapsed);

		for (int i = 0; i < elements.size(); i++) {
			Element element = elements.elementAt(i);
			if ((element.isDinamicElement())) {
				if (element.getxPos() > canvas.getWidth()
						|| element.getyPos() > canvas.getHeight()) {
					element.setDinamicElement(null);
					System.out.println("Esto pasa?");
				}

				element.update(elapsed);
			}
		}
	}

	private void checkElements() {
		for (int i = 0; i < elements.size(); i++) {
			Element element = elements.elementAt(i);
			if (element.getSprite() == null) {
				elements.remove(element);
			}
		}

	}

	private void updateGravity(float elapsed) {

		for (int i = 0; i < elements.size(); i++) {
			Element element = elements.elementAt(i);
			if (element.isDinamicElement()) {
				if (Math.abs(element.getoY()) < gravity * elapsed) {
					element.setLastHitted(null);
					element.setFloor(0);
				}

				element.applySpeed(0, gravity * elapsed);
			}
		}
	}

	private void checkCollides() {

		System.out.println(keyElement.getCenterY() + " " + canvasHeight);

		if (keyElement.getCenterX() > canvasWidth - 10) {
			System.out.println("hola");
			keyElement.reactToColition(new Point(keyElement.getCenterX() + 20,
					keyElement.getCenterY()));
		} else if (keyElement.getCenterX() < 10) {
			System.out.println("hola1");
			keyElement.reactToColition(new Point(keyElement.getCenterX() - 20,
					keyElement.getCenterY()));
		} else if ((keyElement.getCenterY() > canvasHeight - 10)
				&& (keyElement.getFloor() == 0)) {
			System.out.println("hola2");
			keyElement.reactToColition(new Point(keyElement.getCenterX(),
					keyElement.getCenterY() + 20));
			keyElement.setFloor(1);
		} else if (keyElement.getCenterY() < 10) {
			System.out.println("hola3");
			keyElement.reactToColition(new Point(keyElement.getCenterX(),
					keyElement.getCenterY() - 20));

		}

		for (int i = 0; i < elements.size(); i++) {
			Element element = elements.elementAt(i);
			if (!(keyElement.getLastHitted() == element)) {
				Motor.colision(element, keyElement);
			}
		}
	}

	public void hitBall(float touchX, float touchY) {

		float difX = touchX - keyElement.getCenterX();
		if (difX > 100) {
			difX = 100;
		} else if (difX < -100) {
			difX = -100;
		}
		float difY = touchY - keyElement.getCenterY();
		if (difY < 100) {
			difY = 100;
		} else if (difY < -100) {
			difY = -100;
		}
		keyElement.applySpeed(-0.75f * difX, -0.75f * difY);

	}

}
